const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Ex = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const rules = require('./rules');
module.exports = {
  entry: {
    index: './src/js/index.js',
    about: './src/js/about.js'
  },               // 入口文件
  output: {
    filename: 'js/[name].js',
    publicPath: '/',
    path: path.resolve('dist/')
  },              // 出口文件
  resolve: {
    alias: {
      '@': path.resolve('src')
    },
    // 省略后缀
    extensions: ['.js', '.json', '.css']
  },
  externals: {
    jquery: "jQuery"
  },
  module: {
    rules,
  },     // 处理对应模块
  plugins: [
    new CleanWebpackPlugin(['dist']),
    // new webpack.HotModuleReplacementPlugin(),
    /*
      用哪个html作为模板
      在src目录下创建一个index.html页面当做模板来用
      会在打包好的bundle.js后面加上hash串
    */
    new HtmlWebpackPlugin({
      filename: "index.html",
      template: './src/pages/index.html',
      minify: true,
      inject: 'body',
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
      },
      hash: true,
      chunks: ['index']
    }),
    new HtmlWebpackPlugin({
      filename: "about.html",
      template: './src/pages/about.html',
      minify: true,
      inject: 'body',
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
      },
      hash: true,
      chunks: ['about']
    }),
    new Ex("css/[name].[hash:7].css"),
  ],             // 对应的插件
  optimization: {
    splitChunks: {
      chunks: 'initial', // 只对入口文件处理
        cacheGroups: {
            vendor: {   // 抽离第三方插件
                test: /node_modules/,   // 指定是node_modules下的第三方包
                chunks: 'initial',
                name: 'vendor',  // 打包后的文件名，任意命名
                // 设置优先级，防止和自定义的公共代码提取时被覆盖，不进行打包
                priority: 10
            },
            utils: { // 抽离自己写的公共代码，utils这个名字可以随意起
                chunks: 'initial',
                name: 'utils',  // 任意命名
                minSize: 0    // 只要超出0字节就生成一个新包
            }
        }
    }
  },
  devServer: {
    contentBase: path.join(__dirname, "dist/"),
    historyApiFallback: true,
    host: 'localhost',
    open: true,
    inline: true,
    port: 8000,
    compress: true,//服务器返回给浏览器的时候是否启动gzip压缩
  },           // 开发服务器配置
  // mode: 'development'      // 模式配置
  mode: 'production'
}